<?php

/**
 * 图片投放管理MODEL类
 * 
 * @author Dean
 *
 */
class vbcModel extends Model{
	
	//操作的本类表名
	private $tableName = 'launch_group';
	
	/**
	 * 查询全部投放信息
	 */
	public function findAll($limit=0, $num=20, $params=array()){
		
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= "AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_adsnum'])){
			$select_by_sql .= " AND `adsnum` like '%{$params['v_adsnum']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			$select_by_sql .= " AND `keywords` like '%{$params['v_keywords']}%' ";
		}
		if (!empty($params['v_num'])){
			$select_by_sql .= " AND `num` like '%{$params['v_num']}%' ";
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		
		$sql = 'select * from '.tblName($this->tableName).' WHERE 1=1 '.$select_by_sql.'ORDER BY createTime desc LIMIT '.$limit.' , '.$num;
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}
	
	/**
	 * 统计投放总数量
	 * 
	 * @param unknown_type $type
	 */
	public function countAll($params=array()){
		
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= "AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			$select_by_sql .= " AND `keywords` like '%{$params['v_keywords']}%' ";
		}
		if (!empty($params['v_num'])){
			$select_by_sql .= " AND `num` like '%{$params['v_num']}%' ";
		}
		if (!empty($params['v_adsnum'])){
			$select_by_sql .= " AND `adsnum` like '%{$params['v_adsnum']}%' ";
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		if (!empty($params['v_target_window'])){
			return 0;
		}
		if (!empty($params['v_size'])){
			$select_by_sql .= " AND `size` like '%{$params['v_size']}%' ";
		}
		
		$sql = 'select count(*) as num from '.tblName($this->tableName) .' WHERE 1=1 '.$select_by_sql; ;
		
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	/**
	 * 查询单个投放信息，通过投放编号进行查询
	 * 
	 * @param int $id 投放编号
	 */
	public function findOne($id){
		
		$sql = 'select * from '.tblName($this->tableName).' WHERE ID = \''.$id.'\'';
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	
	/**
	 * 进行投放新增
	 * 
	 * @param Array $array
	 * @param int $returnID
	 */
	public function add($array, $returnID = 1){
		
		$sql = insertTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $this->db->lastInsertID;
		}else {
			throw new Exception('投放添加失败。');
			return -1;
		}
	}
	
	/**
	 * 进行修改操作
	 * 
	 * @param Array $array
	 * @param Array $where
	 */
	public function modify($array, $where){
		$sql = updateTableSql($this->tableName, $array, $where);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	/**
	 * 进行删除操作
	 * 
	 * @param Array $array
	 */
	public function delete($array){
		
		$sql = deleteTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	public function getMaxID(){
		
		$sql = "select id from ".tblName($this->tableName)." order by id desc limit 1";
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function findVBCAdsByID($adsID){
		
		$sql = "select * from tbl_ads where id in ($adsID)";
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}
	
	public function findByName($name){
		
		$sql = "select * from ".tblName($this->tableName)." where name='$name' limit 1";
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function findNullOrderGroup(){
		$sql = "select * from ".tblName($this->tableName)." where `order` = '' ";
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}

}

?>